package com.bafa.front;

import com.bafa.dbcache.LeagueCache;
import com.bafa.exceptions.ErrorException;
import com.bafa.front.presentationbeans.MatchBean;
import com.bafa.front.presentationbeans.PlayerListBean;
import com.bafa.front.utils.FrontUtils;
import com.bafa.objects.Match;
import com.bafa.objects.PageBean;
import com.bafa.objects.Report;
import com.bafa.objects.Team;
import com.bafa.services.RetrieveServices;
import com.bafa.services.ReportServices;

import java.io.IOException;

import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ViewMatchReportServlet extends DaddyServlet {
    public ViewMatchReportServlet() {
    }
    
    @Override
    public void doEvent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ErrorException {
            
            HttpSession session = request.getSession();
            Iterator report = ReportServices.getReport(new Integer(request.getParameter("matchId")));
            MatchBean match = new MatchBean(RetrieveServices.getMatch(new Integer(request.getParameter("matchId"))));
			
			request.setAttribute("REPORT",report);
        request.setAttribute("MATCH",match);
       PageBean pageBeanTemp = null;
        if(FrontUtils.isLoggedOnAsAdmin(request)){
            pageBeanTemp = (PageBean)LeagueCache.getPages().get("admineditreport");
           
        }
        else if(FrontUtils.getLoggedOnTeam(request)!=null&&(FrontUtils.getLoggedOnTeam(request).getId()==match.getTeamA()||FrontUtils.getLoggedOnTeam(request).getId()==match.getTeamB()))
        {
			PlayerListBean players = new PlayerListBean(RetrieveServices.getTeam(FrontUtils.getLoggedOnTeam(request).getId()+"").getStats().scorers());
			
			request.setAttribute("PLAYERS",players);
            pageBeanTemp = (PageBean)LeagueCache.getPages().get("vieweditreport"); 
            request.setAttribute("REPORT",ReportServices.getReport(new Integer(request.getParameter("matchId")),FrontUtils.getLoggedOnTeam(request).getId()));
        }else{
            pageBeanTemp = (PageBean)LeagueCache.getPages().get("viewreport");
        }
        
        pageBean = new PageBean(pageBeanTemp);
        String pageHeader = pageBean.getPageheader().replaceAll("<>",match.day()+" "+match.month()+" "+match.year()+" "+match.getTeamAName()+" "+match.getGoalsA()+"-"+match.getGoalsB()+" "+match.getTeamBName());
        pageBean.setPageheader(pageHeader);
        pageBean.setMainheader(pageHeader);
            
            
            
    
    }
    
    
}
